<?php

include dirname(dirname(__FILE__)) . '/include/common.php';

class webServiceForgotPassword extends core {

    public function process() {
        $email = $this->_cleanEmail($_POST['userEmail']);
        $data = $this->db->get_row("SELECT * FROM users WHERE  email='$email'");
        
        if ($data) {
            $new_password = substr(md5(uniqid()), 0, 10);
            $qry = "Update users Set password='" . md5($new_password) . "' Where  id=" . $data->id . "";
            $result= $this->db->query($qry);           
            if ($result) {
                 $subject = "Your request to reset your password";
                 $messages='You have requested that your password be resent. Your password is: ' . $new_password . ' <br>We recommend that you should change your password immediately upon logging into your application.<br> Thank you.<br> The Wedding Helper Application Team';
                if ($this->send_mail($email, $subject, $messages)) {
                    $this->_JSONResponse(array('error' => 0,'msg'=>'Your password has been sent to your email. Please check your inbox.'));
                }else{
                   $this->_JSONResponse(array('error' => 1,'msg'=>'Failed sending email reset password.'));
                }
                
            } else {
                $this->_JSONResponse(array('error' => 1,'msg'=>'Oops, something went wrong. Please try again.'));
            }
            
        } else {
            $this->_JSONResponse(array('error' => 1,'msg'=>'User does not exist'));
        }
    }

}

$processor = new webServiceForgotPassword();
$processor->process();
?>